﻿Imports Microsoft.Office.Interop.Outlook

Namespace UI
    Public Class pmToolbar

        Private _app As Outlook.Application
        Private _cbBar As Office.CommandBar

        Private WithEvents _cbMoveButton As Office.CommandBarButton
        Private WithEvents _cbUndoButton As Office.CommandBarButton
        Private WithEvents _cbFolderButton As Office.CommandBarButton
        Private WithEvents _cbSearchButton As Office.CommandBarButton
        Private WithEvents _cbExcelButton As Office.CommandBarButton
        Private WithEvents _cbFollowUpButton As Office.CommandBarButton
        Private WithEvents _cbReceivedMessagesButton As Office.CommandBarButton
        Private WithEvents _cbSendedMessagesButton As Office.CommandBarButton
        Private WithEvents _cbAllMessagesButton As Office.CommandBarButton

        Private WithEvents _cbReactionsTodayButton As Office.CommandBarButton
        Private WithEvents _cbReactionsThisWeekButton As Office.CommandBarButton
        Private WithEvents _cbReactionsNextWeekButton As Office.CommandBarButton

     

        Friend Sub New(ByVal App As Outlook.Application)
            ' Store a reference to the Outlook.Application object.
            _app = App

            ' Create a WYWO command bar button to show the While You Were Out dialog box.
            InitializeUI()
        End Sub

        Private Sub InitializeUI()
            ' Get a reference to the active Explorer object.
            Dim explorer As Outlook.Explorer = _app.ActiveExplorer

            ' Create a new (temporary) CommandBar object.
            _cbBar = explorer.CommandBars.Add("PMToolbar", Office.MsoBarPosition.msoBarTop, False, True)

            ' Create movebutton
            _cbMoveButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbMoveButton
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "Naar map verplaatsen"
                .FaceId = 1676
            End With

            '' Create undobutton
            '_cbUndoButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            'With _cbUndoButton
            '    .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            '    .Caption = "Verplaatsen ongedaan maken"
            '    .FaceId = 128
            'End With

            ' Create folderbutton
            _cbFolderButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbFolderButton
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "Bijhorende map openen"
                .FaceId = 1661
            End With

            ' Create searchbutton
            _cbSearchButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbSearchButton
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "Zoek klant/order"
                .FaceId = 1744
                '.FaceId = 1791
                .BeginGroup = True
            End With

            ' Create excelbutton
            _cbExcelButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbExcelButton
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "Distripost reservatie"
                '.FaceId = 263
                .TooltipText = "Distripost Online reservatietool"
                .FaceId = 610
                .BeginGroup = True
            End With

            ' Create followUpButton
            _cbFollowUpButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbFollowUpButton
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "Reactie binnen 48 uur"
                .FaceId = 1678
            End With

            ' Create ReceivedMessagesButton
            _cbReceivedMessagesButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbReceivedMessagesButton
                .Style = Office.MsoButtonStyle.msoButtonIcon
                .FaceId = 238
                .Caption = "Ontvangen berichten"
                .BeginGroup = True
            End With

            ' Create AllMessagesButton
            _cbAllMessagesButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbAllMessagesButton
                .Style = Office.MsoButtonStyle.msoButtonIcon
                .FaceId = 237
                .Caption = "Berichten"
                .State = Microsoft.Office.Core.MsoButtonState.msoButtonDown
            End With

            ' Create SendedMessagesButton
            _cbSendedMessagesButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbSendedMessagesButton
                .Style = Office.MsoButtonStyle.msoButtonIcon
                .FaceId = 239
                .Caption = "Verzonden berichten"
            End With

            ' Create ReactionsTodayButton
            _cbReactionsTodayButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbReactionsTodayButton
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "Vandaag"
                .FaceId = 1678
                .BeginGroup = True
                .TooltipText = "Reacties tegen vandaag"
            End With

            ' Create ReactionsThisWeekButton
            _cbReactionsThisWeekButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbReactionsThisWeekButton
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "Deze week"
                .FaceId = 1678
                .TooltipText = "Reacties tegen deze week"
            End With
           
            ' Create ReactionsNextWeekButton
            _cbReactionsNextWeekButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
            With _cbReactionsNextWeekButton
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "Volgende week"
                .FaceId = 1678
                .TooltipText = "Reacties tegen volgende week"
            End With

            ' Make the new CommandBar visible.
            _cbBar.Visible = True

            explorer = Nothing

        End Sub

        Private Sub _cbMoveButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbMoveButton.Click
            Try
                Dim selectedMail As Outlook.MailItem = GetSelectedMail()
                If selectedMail IsNot Nothing Then
                    Using _pmMoveMailForm As pmMoveMailForm = New pmMoveMailForm(Me._app, GetSelectedMail)
                        _pmMoveMailForm.ShowDialog()
                    End Using
                End If
            Catch ex As System.Exception
                ExceptionHandler.ShowAndLog("Fout bij openen van MoveMailForm", ex)
            End Try
        End Sub


        Private Function GetSelectedMail() As Outlook.MailItem
            Try
                If IsNothing(_app.ActiveExplorer.Selection) OrElse _app.ActiveExplorer.Selection.Count <> 1 Then
                    Return Nothing
                End If

                Dim selectedObject As Object = _app.ActiveExplorer.Selection.Item(1)

                If (TypeOf selectedObject Is Outlook.MailItem) Then
                    Return DirectCast(selectedObject, MailItem)
                End If

                Return Nothing
            Catch ex As System.Exception
                Return Nothing
            End Try
        End Function

        Private Sub _cbFollowUpButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbFollowUpButton.Click
            Dim selectedMail As Outlook.MailItem = GetSelectedMail()

            If Not IsNothing(selectedMail) Then
                selectedMail.FlagDueBy = SharedFunctions.DateWithin48Workhours(selectedMail.ReceivedTime)
                selectedMail.FlagIcon = Outlook.OlFlagIcon.olRedFlagIcon
                selectedMail.Save()
            End If
        End Sub

        Private Sub _cbExcelButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbExcelButton.Click
            Try
                SharedMethods.openDistripostInfofiche()

            Catch ex As System.Exception
                ExceptionHandler.ShowAndLog("Fout bij openen van de Distripost infofiche", ex)
            End Try
        End Sub

        Private Sub _cbSearchButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbSearchButton.Click
            Try
                Using _pmSearchFolderForm As pmSearchFolderForm = New pmSearchFolderForm(Me._app)
                    _pmSearchFolderForm.ShowDialog()
                End Using
            Catch ex As System.Exception
                ExceptionHandler.ShowAndLog("Fout bij openen van SearchFolderForm", ex)
            End Try
        End Sub

        Private Sub _cbFolderButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbFolderButton.Click
            Dim selectedMail As Outlook.MailItem = GetSelectedMail()

            If Not IsNothing(selectedMail) Then
                Dim parentfolder As Outlook.MAPIFolder
                If (TypeOf selectedMail.Parent Is Outlook.MAPIFolder) Then
                    parentfolder = CType(selectedMail.Parent, Outlook.MAPIFolder)
                    SharedMethods.SetCurrentFolder(_app, parentfolder)
                End If
            End If
        End Sub

        Private Sub _cbReceivedMessagesButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbReceivedMessagesButton.Click
            Dim vws As Outlook.Views = SharedFunctions.GetInboxFolder(Me._app).Views
            SetMessagesButtons(Microsoft.Office.Core.MsoButtonState.msoButtonUp)
            If vws.Item("Ontvangen berichten") IsNot Nothing Then
                _app.ActiveExplorer.CurrentView = vws.Item("Ontvangen berichten")

                _cbReceivedMessagesButton.State = Microsoft.Office.Core.MsoButtonState.msoButtonDown
            Else
                MessageBox.Show("De weergave 'Ontvangen berichten' bestaat niet.")
            End If
        End Sub

        Private Sub _cbSendedMessagesButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbSendedMessagesButton.Click
            Dim vws As Outlook.Views = SharedFunctions.GetInboxFolder(Me._app).Views

            SetMessagesButtons(Microsoft.Office.Core.MsoButtonState.msoButtonUp)
            If vws.Item("Verzonden berichten") IsNot Nothing Then
                _app.ActiveExplorer.CurrentView = vws.Item("Verzonden berichten")
                _cbSendedMessagesButton.State = Microsoft.Office.Core.MsoButtonState.msoButtonDown
            Else
                MessageBox.Show("De weergave 'Verzonden berichten' bestaat niet.")
            End If
        End Sub

        Private Sub _cbAllMessagesButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbAllMessagesButton.Click
            Dim vws As Outlook.Views = SharedFunctions.GetInboxFolder(Me._app).Views

            SetMessagesButtons(Microsoft.Office.Core.MsoButtonState.msoButtonUp)

            If vws.Item("Berichten") IsNot Nothing Then
                _app.ActiveExplorer.CurrentView = vws.Item("Berichten")
                _cbAllMessagesButton.State = Microsoft.Office.Core.MsoButtonState.msoButtonDown
            Else
                MessageBox.Show("De weergave 'Berichten' bestaat niet.")
            End If
        End Sub

        Private Sub SetMessagesButtons(ByVal state As Microsoft.Office.Core.MsoButtonState)
            _cbReceivedMessagesButton.State = Microsoft.Office.Core.MsoButtonState.msoButtonUp
            _cbAllMessagesButton.State = Microsoft.Office.Core.MsoButtonState.msoButtonUp
            _cbSendedMessagesButton.State = Microsoft.Office.Core.MsoButtonState.msoButtonUp
        End Sub

        Private Sub _cbReactionsTodayButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbReactionsTodayButton.Click
            SharedMethods.SetCurrentFolder(_app, _app.GetNamespace("mapi").GetFolderFromID(ConfigurationHelper.ReactionsTodayEntryID))
        End Sub

        Private Sub _cbReactionsThisWeekButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbReactionsThisWeekButton.Click
            SharedMethods.SetCurrentFolder(_app, _app.GetNamespace("mapi").GetFolderFromID(ConfigurationHelper.ReactionsThisWeekEntryID))
        End Sub

        Private Sub _cbReactionsNextWeekButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbReactionsNextWeekButton.Click
            SharedMethods.SetCurrentFolder(_app, _app.GetNamespace("mapi").GetFolderFromID(ConfigurationHelper.ReactionsNextWeekEntryID))
        End Sub
    End Class
End Namespace